package com.eastfair.visitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.echo.core.EchoService;
import com.eastfair.visitor.dto.DepartmentPageQuery;
import com.eastfair.visitor.dto.DepartmentSaveDTO;
import com.eastfair.visitor.dto.DepartmentUpdateDTO;
import com.eastfair.visitor.entity.Department;
import com.eastfair.visitor.service.DepartmentService;
import com.eastfair.visitor.vo.DepartmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 部门管理
 * </p>
 *
 * @author lyw
 * @date 2022-11-18
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/department")
@Api(value = "Department", tags = "部门管理")
@PreAuth(replace = "visitor:department:")
public class DepartmentController extends SuperController<DepartmentService, Long, Department, DepartmentPageQuery, DepartmentSaveDTO, DepartmentUpdateDTO> {

    @Autowired
    private EchoService echoService;
    @Autowired
    private DepartmentService departmentService;

    @Override
    public void handlerResult(IPage<Department> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Department> departmentList = list.stream().map((map) -> {
            Department department = Department.builder().build();
            //TODO 请在这里完成转换
            return department;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(departmentList));
    }



    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<DepartmentPageQuery> params) {
        DepartmentPageQuery pageQuery = params.getModel();

    }


    /**
     * 查询部门列表分页
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "查询部门列表分页")
    @PostMapping(value = "/queryDepartmentList")
    public R<IPage<DepartmentVO>> queryDepartmentList(@RequestBody PageParams<DepartmentPageQuery> params) {

        IPage<DepartmentVO> page = departmentService.queryDepartmentList(params);
        return R.success(page);

    }

    /**
     * 查询部门列表
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询部门列表")
    @GetMapping(value = "/getDepartmentList")
    public R<List<DepartmentVO>> getDepartmentList() {
        List<DepartmentVO> departmentList = departmentService.getDepartmentList();
        return R.success(departmentList);

    }


    /**
     * 添加部门
     * @param departmentSaveDTO
     * @return
     */
    @ApiOperation(value = "添加部门")
    @PostMapping(value = "/saveDepartmentInfo")
    public R<Boolean> saveDepartmentInfo (@RequestBody  DepartmentSaveDTO departmentSaveDTO) {

        return R.success(departmentService.saveDepartmentInfo(departmentSaveDTO));
    }

    /**
     * 修改部门
     *
     * @return
     */
    @ApiOperation(value = "修改部门")
    @PostMapping(value = "/updateDepartmentInfo")
    public R<Boolean> updateDepartmentInfo(@RequestBody DepartmentUpdateDTO departmentUpdateDTO){

        return R.success(departmentService.updateDepartmentInfo(departmentUpdateDTO));
    }

    /**
     * 逻辑删除部门
     *
     * @return
     */
    @ApiOperation(value = "逻辑删除部门")
    @PostMapping(value = "/delDepartmentById")
    public R<Boolean> delDepartmentById(@RequestBody DepartmentUpdateDTO departmentUpdateDTO) {
        return R.success(departmentService.delDepartmentById(departmentUpdateDTO));
    }

}
